//
//  ViewController.m
//  王宇先_15_文件读写
//
//  Created by wyx on 16/3/29.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "SandBox.h"
#import "Person.h"
#import "Dog.h"

@interface ViewController ()
@property (nonatomic, copy)NSString *pathOfData;
@property (nonatomic, copy)NSString *pathOfdir;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    /** 第一题 */
    [self getPath];
    /** 第二题 */
    //[self createFile];
    /** 第三题 */
    //[self createFileForDate];
    /** 第四题 */
    [self savePersonToDisk];
}
#pragma mark - 第一题 获取每个文件路径
- (void)getPath{
    SandBox *sand = [[SandBox alloc] init];
    NSLog(@"%@", sand.pathOfAppSandBox);
    NSLog(@"%@", sand.pathOfTemp);
    NSLog(@"%@", sand.pathOfPreferences);
    NSLog(@"%@", sand.pathOfLibrary);
    NSLog(@"%@", sand.pathOfCaches);
    NSLog(@"%@", sand.pathOfDocuments);
}
#pragma mark - 第二题
- (void)createFile{
    /** 1 */
    //创建文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //创建文件夹
    NSString *dir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"path"];
    [fileManager createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil];
    //创建. txt
    NSString *dir2 = [dir stringByAppendingPathComponent:@"text.txt"];
    //将照片放入.txt
    UIImage *image = [UIImage imageNamed:@"1.png"];
    NSData *data = UIImageJPEGRepresentation(image, 1);
    [data writeToFile:dir2 atomically:YES];
    /** 2 */
    //在 tmp文件中创建 testDirectory
    NSString *dir3 = [NSTemporaryDirectory()stringByAppendingPathComponent:@"testDirectory"];
    //复制
    [fileManager copyItemAtPath:dir2 toPath:dir3 error:nil];
}
#pragma mark - 第三题
- (void)createFileForDate{
    
    
    self.pathOfdir = [SandBox saveInDocumentWithFileName:@"time.txt"];
    
    [self time];
}
- (void)saveTime{
    NSDateFormatter *date = [[NSDateFormatter alloc] init];
    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strdate = [date stringFromDate:[NSDate date]];
    [strdate writeToFile:self.pathOfdir atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //NSLog(@"%@", strdate);
    
}
- (void)time{
    NSTimer *timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(saveTime) userInfo:nil repeats:YES];
    [timer fire];
}
#pragma mark - 第四题
- (void)savePersonToDisk{
    Person *per1 = [[Person alloc] init];
    per1.name = @"Tom";
    per1.age = 18;
    per1.dog.col = @"黄";
    per1.dog.syl = @"金毛";
    
    //归档
    NSMutableData *mData = [NSMutableData data];
    NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    [archive encodeObject:per1 forKey:@"model"];
    //归档结束
    [archive finishEncoding];
    [mData writeToFile:[SandBox saveInDocumentWithFileName:@"model.da"] atomically:YES];
}
#pragma mark - 其他
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
